package com.zhongxi.guoyuan.adapter.rewardCoin;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.ItemGoodAttr;
import com.zhongxi.guoyuan.entity.coinStore.SelcetPostCartGoodsAttrs;
import com.zhongxi.guoyuan.entity.coinStore.ShopInfos;
import com.zhongxi.guoyuan.fragment.cartstore.PostStoreFragment;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.DataUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/22.
 */

public class RewardShoppingCartAdapter extends BaseAdapter {
    private List<String> amountofmaillist;
    private PostStoreFragment fragment;
    private List<Object> list;
    private Handler handler;
    private List<ShopInfos> keyList;
    private Context context;
    private boolean isAllChecked;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_SEPARATOR = 1;
    private static final int TYPE_FREE = 2;
    private static final int TYPE_MAX_COUNT = TYPE_FREE + 1;
    private Map<Integer, View> mapView = new HashMap<>();

    public RewardShoppingCartAdapter(Context context, PostStoreFragment fragment, List<Object> list, List<ShopInfos> keyList, List<String> amountofmaillist, Handler handler) {
        this.context = context;
        this.fragment = fragment;
        this.keyList = keyList;
        this.handler = handler;
        this.list = list;
        this.amountofmaillist = amountofmaillist;
    }

    public boolean isAllChecked() {
        return isAllChecked;
    }

    public void setAllChecked(boolean allChecked) {
        isAllChecked = allChecked;
    }

    public int getItemViewType(int position) {
        if (keyList.contains(list.get(position))) {
            return TYPE_ITEM;
        } else if (amountofmaillist.contains(list.get(position))) {
            return TYPE_FREE;
        } else {
            return TYPE_SEPARATOR;
        }
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public boolean isEnabled(int position) {
        if (keyList.contains(getItem(position))) {
            return false;
        }
        return super.isEnabled(position);
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder vh;
        int type = getItemViewType(i);
        if (mapView.get(i) == null) {
            vh = new ViewHolder();
            switch (type) {
                case TYPE_ITEM:
                    view = View.inflate(context, R.layout.item_post_cart, null);
                    vh.postcart_shop_name = (TextView) view.findViewById(R.id.postcart_shop_name);
                    vh.tv_num = (TextView) view.findViewById(R.id.tv_num);
                    vh.view = view.findViewById(R.id.view);
                    break;
                case TYPE_SEPARATOR:
                    view = View.inflate(context, R.layout.item_reward_shopping_cart, null);
                    vh.ivImg = (ImageView) view.findViewById(R.id.ivImg);
                    vh.tvName = (TextView) view.findViewById(R.id.tvName);
                    vh.tvAdd = (TextView) view.findViewById(R.id.tvAdd);
                    vh.tvSub = (TextView) view.findViewById(R.id.tvSub);
                    vh.tvMoney = (TextView) view.findViewById(R.id.tvMoney);
                    vh.etNum = (EditText) view.findViewById(R.id.etNum);
                    vh.ivCheck = (ImageView) view.findViewById(R.id.ivSelect);
                    vh.tvEdit = (TextView) view.findViewById(R.id.tvEdit);
                    vh.tvNature = (TextView) view.findViewById(R.id.tvNature);
                    vh.tv_item_md = (TextView) view.findViewById(R.id.tv_item_md);
                    vh.rl_item_cart = (LinearLayout) view.findViewById(R.id.rl_item_cart);
                    break;
                case TYPE_FREE:
                    view = View.inflate(context, R.layout.item_post_cart_free, null);
                    vh.tv_amountofmail = (TextView) view.findViewById(R.id.tv_amountofmail);
            }
            mapView.put(i, view);
            view.setTag(vh);
        } else {
            view = mapView.get(i);
            vh = (ViewHolder) view.getTag();
        }
        if (keyList.contains(list.get(i))) {
            if (vh.postcart_shop_name != null) {
                vh.postcart_shop_name.setText(((ShopInfos) getItem(i)).getName());
                vh.tv_num.setText(((ShopInfos) getItem(i)).getStoreNum() + "件");
                if (keyList.size() > 1) {
                    if (i == 0) {
                        vh.view.setVisibility(View.GONE);
                    } else {
                        vh.view.setVisibility(View.VISIBLE);
                    }
                } else {
                    vh.view.setVisibility(View.GONE);
                }

            }
        } else if (amountofmaillist.contains(list.get(i))) {
            String amountofmail = (String) getItem(i);
            vh.tv_amountofmail.setText(amountofmail);
        } else {
            if (vh.ivImg != null) {
                final SelcetPostCartGoodsAttrs item = (SelcetPostCartGoodsAttrs) getItem(i);
                if (item.getImgUrl() != null && !"".equals(item.getImgUrl())) {
                    Glide.with(context).load(Url.IMG_URL + item.getImgUrl()).fitCenter().placeholder(R.mipmap.iv_eg).into(vh.ivImg);
                }
                if (item.getName() != null && !"".equals(item.getName())) {
                    vh.tvName.setText(item.getName());
                }
                if (item.getPrice() != null && !"".equals(item.getPrice())) {
                    vh.tvMoney.setText(item.getPrice() + "元 ");
                } else {
                    vh.tvMoney.setText("0元 ");
                }
                switch (item.getGoodstype()) {
                    case "1":
                        if (item.getPrice() != null && !"".equals(item.getPrice())) {
                            if (DataUtil.moreThanZero(item.getPrice())) {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                    vh.tv_item_md.setText(Html.fromHtml("<font color='#ff7f00'>" + context.getResources().getString(R.string.give_meidou) +
                                            CommonTool.getWhole(item.getPrice()) + "美豆<br/></font><font color='#ff4c4c'>" + context.getResources().getString(R.string.more_give) + DataUtil.multiplys(item.getPrice(), "0.05", 2) + "红豆<font>", Html.FROM_HTML_MODE_COMPACT));
                                } else {
                                    vh.tv_item_md.setText(Html.fromHtml("<font color='#ff7f00'>" + context.getResources().getString(R.string.give_meidou) +
                                            CommonTool.getWhole(item.getPrice()) + "美豆<br/></font><font color='#ff4c4c'>" + context.getResources().getString(R.string.more_give) + DataUtil.multiplys(item.getPrice(), "0.05", 2) + "红豆<font>"));
                                }
                            } else {
                                vh.tv_item_md.setText("");
                            }
                        } else {
                            vh.tv_item_md.setText("");
                        }
                        break;
                    case "2":
                        if (item.getPrice() != null && !"".equals(item.getPrice())) {
//                            vh.tv_item_md.setText("立即赠:" + DataUtil.multiplys(item.getPrice(), "2") + "美豆");
                            vh.tv_item_md.setText("");
                        } else {
                            vh.tv_item_md.setText("");
                        }
                        break;
                    case "3":
                        if (item.getPrice() != null && !"".equals(item.getPrice())) {
                            if (DataUtil.moreThanZero(item.getPrice())) {
                                vh.tv_item_md.setText(context.getResources().getString(R.string.award_hongdou) + item.getPrice() + "红豆");
                                vh.tv_item_md.setTextColor(context.getResources().getColor(R.color.title_red));
                            } else {
                                vh.tv_item_md.setText("");
                            }
                        } else {
                            vh.tv_item_md.setText("");
                        }
                        break;
                }
                if (item.getGoodsnum() != null && !"".equals(item.getGoodsnum())) {
                    vh.etNum.setText(item.getGoodsnum());
                }
                if (item.getItemGoodAttrs() != null && item.getItemGoodAttrs().size() > 0) {
                    List<ItemGoodAttr> itemGoodAttrs = item.getItemGoodAttrs();
                    String tvNature = "";
                    for (int j = 0; j < itemGoodAttrs.size(); j++) {
                        tvNature += itemGoodAttrs.get(j).getAttrv() + "/";
                    }
                    vh.tvNature.setText("规格:" + tvNature.substring(0, tvNature.length() - 1));
                }

                if (item.isSelected()) {
                    vh.ivCheck.setImageResource(R.mipmap.yes);
                } else {
                    vh.ivCheck.setImageResource(R.mipmap.no);
                }

                if (item.getStatus().equals("0") || item.getItemStatus().equals("0") || item.getItemGoodAttrs() == null || item.getItemGoodAttrs().size() == 0 || "0".equals(item.getStocknum()) || item.getStocknum() == null) {
                    vh.rl_item_cart.setBackgroundResource(R.drawable.bg_cart_soldout_square);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            final AlertDialog.Builder adb = new AlertDialog.Builder(context);
                            adb.setMessage("该商品已下架,是否删除该商品");
                            adb.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int position) {
                                    Message message = new Message();
                                    message.what = 1;
                                    message.obj = i;
                                    handler.sendMessage(message);
                                }
                            });
                            adb.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int position) {
                                    adb.create().dismiss();
                                }
                            });
                            adb.show();
                        }
                    });
                } else {
                    vh.rl_item_cart.setBackgroundResource(R.drawable.bg_cart_square);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                        }
                    });
                }

                vh.ivCheck.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (item.getStatus().equals("0") || item.getItemStatus().equals("0") || item.getItemGoodAttrs() == null || item.getItemGoodAttrs().size() == 0 || "0".equals(item.getStocknum()) || item.getStocknum() == null) {
                            item.setSelected(false);
                            if (item.getItemStatus().equals("0")) {
                                Toast.makeText(context, "该商品已下架暂不能勾选", Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(context, "该店铺停业休整,店铺商品暂不能勾选", Toast.LENGTH_LONG).show();
                            }
                        } else {
                            item.setGoodsnum(vh.etNum.getText().toString().trim());
                            if (!item.isSelected()) {
                                item.setSelected(true);
                                vh.ivCheck.setImageResource(R.mipmap.yes);
                            } else {
                                item.setSelected(false);
                                vh.ivCheck.setImageResource(R.mipmap.no);
                                isAllChecked = false;
                            }
                            fragment.updateAmount();
                        }
                    }
                });
                vh.tvSub.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (Integer.parseInt(vh.etNum.getText().toString().trim()) > 1) {
                            vh.etNum.setText(Integer.parseInt(vh.etNum.getText().toString().trim()) - 1 + "");
                        } else {
                            vh.etNum.setText("1");
                        }
                        item.setGoodsnum(vh.etNum.getText().toString().trim());
                        if (item.isSelected()) {
                            fragment.updateAmount();
                        }
                        Message message = new Message();
                        message.what = 2;
                        message.obj = i;
                        handler.sendMessage(message);
                    }
                });
                vh.tvAdd.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (TextUtils.isEmpty(vh.etNum.getText().toString().trim())) {
                            vh.etNum.setText("1");
                        } else {
                            vh.etNum.setText(Integer.parseInt(vh.etNum.getText().toString().trim()) + 1 + "");
                        }
                        item.setGoodsnum(vh.etNum.getText().toString().trim());
                        if (item.isSelected()) {
                            fragment.updateAmount();
                        }
                        Message message = new Message();
                        message.what = 3;
                        message.obj = i;
                        handler.sendMessage(message);
                    }
                });
                vh.tvEdit.setOnClickListener(new android.view.View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Message message = new Message();
                        message.what = 0;
                        message.obj = i;
                        handler.sendMessage(message);
                    }
                });

            }
        }
        return view;
    }

    class ViewHolder {
        private TextView postcart_shop_name;
        private TextView tv_num;
        private TextView tvName;
        private EditText etNum;
        private TextView tvMoney;
        private TextView tvAdd;
        private TextView tvSub;
        private ImageView ivCheck;
        private TextView tvEdit;
        private TextView tvNature;
        private ImageView ivImg;
        private TextView tv_item_md;
        private View view;
        private LinearLayout rl_item_cart;
        private TextView tv_amountofmail;
    }
}
